{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Filtering Rows"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "lines_to_next_cell": 2
   },
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "import numpy as np\n",
    "pd.set_option('max_columns', 4, 'max_rows', 10, 'max_colwidth', 12)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Introduction"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Calculating boolean statistics"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### How to do it..."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "lines_to_next_cell": 2
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>duration</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>movie_title</th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>Avatar</th>\n",
       "      <td>178.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Pirates of the Caribbean: At World's End</th>\n",
       "      <td>169.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Spectre</th>\n",
       "      <td>148.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>The Dark Knight Rises</th>\n",
       "      <td>164.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Star Wars: Episode VII - The Force Awakens</th>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "              duration\n",
       "movie_title           \n",
       "Avatar           178.0\n",
       "Pirates o...     169.0\n",
       "Spectre          148.0\n",
       "The Dark ...     164.0\n",
       "Star Wars...       NaN"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "movie = pd.read_csv('data/movie.csv', index_col='movie_title')\n",
    "movie[['duration']].head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "lines_to_next_cell": 2
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "movie_title\n",
       "Avatar                                         True\n",
       "Pirates of the Caribbean: At World's End       True\n",
       "Spectre                                        True\n",
       "The Dark Knight Rises                          True\n",
       "Star Wars: Episode VII - The Force Awakens    False\n",
       "John Carter                                    True\n",
       "Spider-Man 3                                   True\n",
       "Tangled                                       False\n",
       "Avengers: Age of Ultron                        True\n",
       "Harry Potter and the Half-Blood Prince         True\n",
       "Name: duration, dtype: bool"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "movie_2_hours = movie['duration'] > 120\n",
    "movie_2_hours.head(10)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "lines_to_next_cell": 2
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1039"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "movie_2_hours.sum()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "lines_to_next_cell": 2
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.2113506916192026"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "movie_2_hours.mean()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "lines_to_next_cell": 2
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.21199755152009794"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "movie['duration'].dropna().gt(120).mean()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {
    "lines_to_next_cell": 2
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "count      4916\n",
       "unique        2\n",
       "top       False\n",
       "freq       3877\n",
       "Name: duration, dtype: object"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "movie_2_hours.describe()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### How it works..."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {
    "lines_to_next_cell": 2
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "False    0.788649\n",
       "True     0.211351\n",
       "Name: duration, dtype: float64"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "movie_2_hours.value_counts(normalize=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {
    "lines_to_next_cell": 2
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "count    4916.000000\n",
       "mean        0.211351\n",
       "std         0.408308\n",
       "min         0.000000\n",
       "25%         0.000000\n",
       "50%         0.000000\n",
       "75%         0.000000\n",
       "max         1.000000\n",
       "Name: duration, dtype: float64"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "movie_2_hours.astype(int).describe()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### There's more..."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {
    "lines_to_next_cell": 2
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.9777687130328371"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "actors = movie[['actor_1_facebook_likes',\n",
    "   'actor_2_facebook_likes']].dropna()\n",
    "(actors['actor_1_facebook_likes'] >\n",
    "      actors['actor_2_facebook_likes']).mean()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Constructing multiple boolean conditions"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### How to do it..."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {
    "lines_to_next_cell": 2
   },
   "outputs": [],
   "source": [
    "movie = pd.read_csv('data/movie.csv', index_col='movie_title')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {
    "lines_to_next_cell": 2
   },
   "outputs": [],
   "source": [
    "criteria1 = movie.imdb_score > 8\n",
    "criteria2 = movie.content_rating == 'PG-13'\n",
    "criteria3 = ((movie.title_year < 2000) |\n",
    "   (movie.title_year > 2009))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {
    "lines_to_next_cell": 2
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "movie_title\n",
       "Avatar                                        False\n",
       "Pirates of the Caribbean: At World's End      False\n",
       "Spectre                                       False\n",
       "The Dark Knight Rises                          True\n",
       "Star Wars: Episode VII - The Force Awakens    False\n",
       "dtype: bool"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "criteria_final = criteria1 & criteria2 & criteria3\n",
    "criteria_final.head()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### How it works..."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### There's more..."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {
    "lines_to_next_cell": 2
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "False"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "5 < 10 and 3 > 4"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {
    "lines_to_next_cell": 2
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "False"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "5 < 10 and 3 > 4"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {
    "lines_to_next_cell": 2
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "False"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "True and 3 > 4"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {
    "lines_to_next_cell": 2
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "False"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "True and False"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {
    "lines_to_next_cell": 2
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "False"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "False"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {
    "lines_to_next_cell": 2
   },
   "outputs": [
    {
     "ename": "TypeError",
     "evalue": "cannot compare a dtyped [float64] array with a scalar of type [bool]",
     "output_type": "error",
     "traceback": [
      "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[0;31mTypeError\u001b[0m                                 Traceback (most recent call last)",
      "\u001b[0;32m~/.env/364/lib/python3.6/site-packages/pandas/core/ops/__init__.py\u001b[0m in \u001b[0;36mna_op\u001b[0;34m(x, y)\u001b[0m\n\u001b[1;32m   1253\u001b[0m         \u001b[0;32mtry\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m-> 1254\u001b[0;31m             \u001b[0mresult\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mop\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mx\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0my\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m   1255\u001b[0m         \u001b[0;32mexcept\u001b[0m \u001b[0mTypeError\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
      "\u001b[0;32m~/.env/364/lib/python3.6/site-packages/pandas/core/ops/roperator.py\u001b[0m in \u001b[0;36mror_\u001b[0;34m(left, right)\u001b[0m\n\u001b[1;32m     56\u001b[0m \u001b[0;32mdef\u001b[0m \u001b[0mror_\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mleft\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mright\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m---> 57\u001b[0;31m     \u001b[0;32mreturn\u001b[0m \u001b[0moperator\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mor_\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mright\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mleft\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m     58\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n",
      "\u001b[0;31mTypeError\u001b[0m: ufunc 'bitwise_or' not supported for the input types, and the inputs could not be safely coerced to any supported types according to the casting rule ''safe''",
      "\nDuring handling of the above exception, another exception occurred:\n",
      "\u001b[0;31mValueError\u001b[0m                                Traceback (most recent call last)",
      "\u001b[0;32m~/.env/364/lib/python3.6/site-packages/pandas/core/ops/__init__.py\u001b[0m in \u001b[0;36mna_op\u001b[0;34m(x, y)\u001b[0m\n\u001b[1;32m   1268\u001b[0m                 \u001b[0;32mtry\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m-> 1269\u001b[0;31m                     \u001b[0mresult\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mlibops\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mscalar_binop\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mx\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0my\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mop\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m   1270\u001b[0m                 except (\n",
      "\u001b[0;32mpandas/_libs/ops.pyx\u001b[0m in \u001b[0;36mpandas._libs.ops.scalar_binop\u001b[0;34m()\u001b[0m\n",
      "\u001b[0;31mValueError\u001b[0m: Buffer dtype mismatch, expected 'Python object' but got 'double'",
      "\nDuring handling of the above exception, another exception occurred:\n",
      "\u001b[0;31mTypeError\u001b[0m                                 Traceback (most recent call last)",
      "\u001b[0;32m<ipython-input-19-8d7f69a8e577>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mmovie\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mtitle_year\u001b[0m \u001b[0;34m<\u001b[0m \u001b[0;36m2000\u001b[0m \u001b[0;34m|\u001b[0m \u001b[0mmovie\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mtitle_year\u001b[0m \u001b[0;34m>\u001b[0m \u001b[0;36m2009\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[0;32m~/.env/364/lib/python3.6/site-packages/pandas/core/ops/__init__.py\u001b[0m in \u001b[0;36mwrapper\u001b[0;34m(self, other)\u001b[0m\n\u001b[1;32m   1319\u001b[0m         \u001b[0;31m#   integer dtypes.  Otherwise these are boolean ops\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m   1320\u001b[0m         \u001b[0mfiller\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mfill_int\u001b[0m \u001b[0;32mif\u001b[0m \u001b[0mis_self_int_dtype\u001b[0m \u001b[0;32mand\u001b[0m \u001b[0mis_other_int_dtype\u001b[0m \u001b[0;32melse\u001b[0m \u001b[0mfill_bool\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m-> 1321\u001b[0;31m         \u001b[0mres_values\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mna_op\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mvalues\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0movalues\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m   1322\u001b[0m         \u001b[0munfilled\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_constructor\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mres_values\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mindex\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mindex\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mname\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0mres_name\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m   1323\u001b[0m         \u001b[0mfilled\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mfiller\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0munfilled\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
      "\u001b[0;32m~/.env/364/lib/python3.6/site-packages/pandas/core/ops/__init__.py\u001b[0m in \u001b[0;36mna_op\u001b[0;34m(x, y)\u001b[0m\n\u001b[1;32m   1278\u001b[0m                         \u001b[0;34m\"cannot compare a dtyped [{dtype}] array \"\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m   1279\u001b[0m                         \"with a scalar of type [{typ}]\".format(\n\u001b[0;32m-> 1280\u001b[0;31m                             \u001b[0mdtype\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0mx\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mdtype\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mtyp\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0mtype\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0my\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m__name__\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m   1281\u001b[0m                         )\n\u001b[1;32m   1282\u001b[0m                     )\n",
      "\u001b[0;31mTypeError\u001b[0m: cannot compare a dtyped [float64] array with a scalar of type [bool]"
     ]
    }
   ],
   "source": [
    "movie.title_year < 2000 | movie.title_year > 2009"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Filtering with boolean arrays"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### How to do it..."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {
    "lines_to_next_cell": 2
   },
   "outputs": [],
   "source": [
    "movie = pd.read_csv('data/movie.csv', index_col='movie_title')\n",
    "crit_a1 = movie.imdb_score > 8\n",
    "crit_a2 = movie.content_rating == 'PG-13'\n",
    "crit_a3 = (movie.title_year < 2000) | (movie.title_year > 2009)\n",
    "final_crit_a = crit_a1 & crit_a2 & crit_a3"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {
    "lines_to_next_cell": 2
   },
   "outputs": [],
   "source": [
    "crit_b1 = movie.imdb_score < 5\n",
    "crit_b2 = movie.content_rating == 'R'\n",
    "crit_b3 = ((movie.title_year >= 2000) &\n",
    "(movie.title_year <= 2010))\n",
    "final_crit_b = crit_b1 & crit_b2 & crit_b3"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {
    "lines_to_next_cell": 2
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "movie_title\n",
       "Avatar                                        False\n",
       "Pirates of the Caribbean: At World's End      False\n",
       "Spectre                                       False\n",
       "The Dark Knight Rises                          True\n",
       "Star Wars: Episode VII - The Force Awakens    False\n",
       "dtype: bool"
      ]
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "final_crit_all = final_crit_a | final_crit_b\n",
    "final_crit_all.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {
    "lines_to_next_cell": 2
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>color</th>\n",
       "      <th>director_name</th>\n",
       "      <th>...</th>\n",
       "      <th>aspect_ratio</th>\n",
       "      <th>movie_facebook_likes</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>movie_title</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>The Dark Knight Rises</th>\n",
       "      <td>Color</td>\n",
       "      <td>Christop...</td>\n",
       "      <td>...</td>\n",
       "      <td>2.35</td>\n",
       "      <td>164000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>The Avengers</th>\n",
       "      <td>Color</td>\n",
       "      <td>Joss Whedon</td>\n",
       "      <td>...</td>\n",
       "      <td>1.85</td>\n",
       "      <td>123000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Captain America: Civil War</th>\n",
       "      <td>Color</td>\n",
       "      <td>Anthony ...</td>\n",
       "      <td>...</td>\n",
       "      <td>2.35</td>\n",
       "      <td>72000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Guardians of the Galaxy</th>\n",
       "      <td>Color</td>\n",
       "      <td>James Gunn</td>\n",
       "      <td>...</td>\n",
       "      <td>2.35</td>\n",
       "      <td>96000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Interstellar</th>\n",
       "      <td>Color</td>\n",
       "      <td>Christop...</td>\n",
       "      <td>...</td>\n",
       "      <td>2.35</td>\n",
       "      <td>349000</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>5 rows × 27 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "              color director_name  ...  aspect_ratio  movie_facebook_likes\n",
       "movie_title                        ...                                    \n",
       "The Dark ...  Color  Christop...   ...         2.35        164000         \n",
       "The Avengers  Color  Joss Whedon   ...         1.85        123000         \n",
       "Captain A...  Color  Anthony ...   ...         2.35         72000         \n",
       "Guardians...  Color   James Gunn   ...         2.35         96000         \n",
       "Interstellar  Color  Christop...   ...         2.35        349000         \n",
       "\n",
       "[5 rows x 27 columns]"
      ]
     },
     "execution_count": 23,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "movie[final_crit_all].head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {
    "lines_to_next_cell": 2
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>color</th>\n",
       "      <th>director_name</th>\n",
       "      <th>...</th>\n",
       "      <th>aspect_ratio</th>\n",
       "      <th>movie_facebook_likes</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>movie_title</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>The Dark Knight Rises</th>\n",
       "      <td>Color</td>\n",
       "      <td>Christop...</td>\n",
       "      <td>...</td>\n",
       "      <td>2.35</td>\n",
       "      <td>164000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>The Avengers</th>\n",
       "      <td>Color</td>\n",
       "      <td>Joss Whedon</td>\n",
       "      <td>...</td>\n",
       "      <td>1.85</td>\n",
       "      <td>123000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Captain America: Civil War</th>\n",
       "      <td>Color</td>\n",
       "      <td>Anthony ...</td>\n",
       "      <td>...</td>\n",
       "      <td>2.35</td>\n",
       "      <td>72000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Guardians of the Galaxy</th>\n",
       "      <td>Color</td>\n",
       "      <td>James Gunn</td>\n",
       "      <td>...</td>\n",
       "      <td>2.35</td>\n",
       "      <td>96000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Interstellar</th>\n",
       "      <td>Color</td>\n",
       "      <td>Christop...</td>\n",
       "      <td>...</td>\n",
       "      <td>2.35</td>\n",
       "      <td>349000</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>5 rows × 27 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "              color director_name  ...  aspect_ratio  movie_facebook_likes\n",
       "movie_title                        ...                                    \n",
       "The Dark ...  Color  Christop...   ...         2.35        164000         \n",
       "The Avengers  Color  Joss Whedon   ...         1.85        123000         \n",
       "Captain A...  Color  Anthony ...   ...         2.35         72000         \n",
       "Guardians...  Color   James Gunn   ...         2.35         96000         \n",
       "Interstellar  Color  Christop...   ...         2.35        349000         \n",
       "\n",
       "[5 rows x 27 columns]"
      ]
     },
     "execution_count": 24,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "movie.loc[final_crit_all].head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {
    "lines_to_next_cell": 2
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>imdb_score</th>\n",
       "      <th>content_rating</th>\n",
       "      <th>title_year</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>movie_title</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>The Dark Knight Rises</th>\n",
       "      <td>8.5</td>\n",
       "      <td>PG-13</td>\n",
       "      <td>2012.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>The Avengers</th>\n",
       "      <td>8.1</td>\n",
       "      <td>PG-13</td>\n",
       "      <td>2012.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Captain America: Civil War</th>\n",
       "      <td>8.2</td>\n",
       "      <td>PG-13</td>\n",
       "      <td>2016.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Guardians of the Galaxy</th>\n",
       "      <td>8.1</td>\n",
       "      <td>PG-13</td>\n",
       "      <td>2014.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Interstellar</th>\n",
       "      <td>8.6</td>\n",
       "      <td>PG-13</td>\n",
       "      <td>2014.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Inception</th>\n",
       "      <td>8.8</td>\n",
       "      <td>PG-13</td>\n",
       "      <td>2010.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>The Martian</th>\n",
       "      <td>8.1</td>\n",
       "      <td>PG-13</td>\n",
       "      <td>2015.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Town &amp; Country</th>\n",
       "      <td>4.4</td>\n",
       "      <td>R</td>\n",
       "      <td>2001.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Sex and the City 2</th>\n",
       "      <td>4.3</td>\n",
       "      <td>R</td>\n",
       "      <td>2010.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Rollerball</th>\n",
       "      <td>3.0</td>\n",
       "      <td>R</td>\n",
       "      <td>2002.0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "              imdb_score content_rating  title_year\n",
       "movie_title                                        \n",
       "The Dark ...         8.5        PG-13        2012.0\n",
       "The Avengers         8.1        PG-13        2012.0\n",
       "Captain A...         8.2        PG-13        2016.0\n",
       "Guardians...         8.1        PG-13        2014.0\n",
       "Interstellar         8.6        PG-13        2014.0\n",
       "Inception            8.8        PG-13        2010.0\n",
       "The Martian          8.1        PG-13        2015.0\n",
       "Town & Co...         4.4            R        2001.0\n",
       "Sex and t...         4.3            R        2010.0\n",
       "Rollerball           3.0            R        2002.0"
      ]
     },
     "execution_count": 25,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "cols = ['imdb_score', 'content_rating', 'title_year']\n",
    "movie_filtered = movie.loc[final_crit_all, cols]\n",
    "movie_filtered.head(10)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### How it works..."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {
    "lines_to_next_cell": 2
   },
   "outputs": [
    {
     "ename": "ValueError",
     "evalue": "iLocation based boolean indexing cannot use an indexable as a mask",
     "output_type": "error",
     "traceback": [
      "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[0;31mValueError\u001b[0m                                Traceback (most recent call last)",
      "\u001b[0;32m<ipython-input-26-ed9199ea5965>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mmovie\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0miloc\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0mfinal_crit_all\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[0;32m~/.env/364/lib/python3.6/site-packages/pandas/core/indexing.py\u001b[0m in \u001b[0;36m__getitem__\u001b[0;34m(self, key)\u001b[0m\n\u001b[1;32m   1422\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m   1423\u001b[0m             \u001b[0mmaybe_callable\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mcom\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mapply_if_callable\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mkey\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mobj\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m-> 1424\u001b[0;31m             \u001b[0;32mreturn\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_getitem_axis\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mmaybe_callable\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0maxis\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0maxis\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m   1425\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m   1426\u001b[0m     \u001b[0;32mdef\u001b[0m \u001b[0m_is_scalar_access\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mkey\u001b[0m\u001b[0;34m:\u001b[0m \u001b[0mTuple\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
      "\u001b[0;32m~/.env/364/lib/python3.6/site-packages/pandas/core/indexing.py\u001b[0m in \u001b[0;36m_getitem_axis\u001b[0;34m(self, key, axis)\u001b[0m\n\u001b[1;32m   2141\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m   2142\u001b[0m         \u001b[0;32mif\u001b[0m \u001b[0mcom\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mis_bool_indexer\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mkey\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m-> 2143\u001b[0;31m             \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_validate_key\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mkey\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0maxis\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m   2144\u001b[0m             \u001b[0;32mreturn\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_getbool_axis\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mkey\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0maxis\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0maxis\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m   2145\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n",
      "\u001b[0;32m~/.env/364/lib/python3.6/site-packages/pandas/core/indexing.py\u001b[0m in \u001b[0;36m_validate_key\u001b[0;34m(self, key, axis)\u001b[0m\n\u001b[1;32m   2004\u001b[0m                     )\n\u001b[1;32m   2005\u001b[0m                 raise ValueError(\n\u001b[0;32m-> 2006\u001b[0;31m                     \u001b[0;34m\"iLocation based boolean indexing cannot use \"\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m   2007\u001b[0m                     \u001b[0;34m\"an indexable as a mask\"\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m   2008\u001b[0m                 )\n",
      "\u001b[0;31mValueError\u001b[0m: iLocation based boolean indexing cannot use an indexable as a mask"
     ]
    }
   ],
   "source": [
    "movie.iloc[final_crit_all]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "metadata": {
    "lines_to_next_cell": 2
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>color</th>\n",
       "      <th>director_name</th>\n",
       "      <th>...</th>\n",
       "      <th>aspect_ratio</th>\n",
       "      <th>movie_facebook_likes</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>movie_title</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>The Dark Knight Rises</th>\n",
       "      <td>Color</td>\n",
       "      <td>Christop...</td>\n",
       "      <td>...</td>\n",
       "      <td>2.35</td>\n",
       "      <td>164000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>The Avengers</th>\n",
       "      <td>Color</td>\n",
       "      <td>Joss Whedon</td>\n",
       "      <td>...</td>\n",
       "      <td>1.85</td>\n",
       "      <td>123000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Captain America: Civil War</th>\n",
       "      <td>Color</td>\n",
       "      <td>Anthony ...</td>\n",
       "      <td>...</td>\n",
       "      <td>2.35</td>\n",
       "      <td>72000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Guardians of the Galaxy</th>\n",
       "      <td>Color</td>\n",
       "      <td>James Gunn</td>\n",
       "      <td>...</td>\n",
       "      <td>2.35</td>\n",
       "      <td>96000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Interstellar</th>\n",
       "      <td>Color</td>\n",
       "      <td>Christop...</td>\n",
       "      <td>...</td>\n",
       "      <td>2.35</td>\n",
       "      <td>349000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>The Young Unknowns</th>\n",
       "      <td>Color</td>\n",
       "      <td>Catherin...</td>\n",
       "      <td>...</td>\n",
       "      <td>NaN</td>\n",
       "      <td>4</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Bled</th>\n",
       "      <td>Color</td>\n",
       "      <td>Christop...</td>\n",
       "      <td>...</td>\n",
       "      <td>1.85</td>\n",
       "      <td>128</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Hoop Dreams</th>\n",
       "      <td>Color</td>\n",
       "      <td>Steve James</td>\n",
       "      <td>...</td>\n",
       "      <td>1.33</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Death Calls</th>\n",
       "      <td>Color</td>\n",
       "      <td>Ken Del ...</td>\n",
       "      <td>...</td>\n",
       "      <td>1.85</td>\n",
       "      <td>16</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>The Legend of God's Gun</th>\n",
       "      <td>Color</td>\n",
       "      <td>Mike Bruce</td>\n",
       "      <td>...</td>\n",
       "      <td>2.35</td>\n",
       "      <td>13</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>86 rows × 27 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "              color director_name  ...  aspect_ratio  movie_facebook_likes\n",
       "movie_title                        ...                                    \n",
       "The Dark ...  Color  Christop...   ...         2.35        164000         \n",
       "The Avengers  Color  Joss Whedon   ...         1.85        123000         \n",
       "Captain A...  Color  Anthony ...   ...         2.35         72000         \n",
       "Guardians...  Color   James Gunn   ...         2.35         96000         \n",
       "Interstellar  Color  Christop...   ...         2.35        349000         \n",
       "...             ...          ...   ...          ...           ...         \n",
       "The Young...  Color  Catherin...   ...          NaN             4         \n",
       "Bled          Color  Christop...   ...         1.85           128         \n",
       "Hoop Dreams   Color  Steve James   ...         1.33             0         \n",
       "Death Calls   Color  Ken Del ...   ...         1.85            16         \n",
       "The Legen...  Color   Mike Bruce   ...         2.35            13         \n",
       "\n",
       "[86 rows x 27 columns]"
      ]
     },
     "execution_count": 43,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "movie.iloc[final_crit_all.values]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### There's more..."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 44,
   "metadata": {
    "lines_to_next_cell": 2
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 44,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "final_crit_a2 = ((movie.imdb_score > 8) & \n",
    "   (movie.content_rating == 'PG-13') & \n",
    "   ((movie.title_year < 2000) |\n",
    "    (movie.title_year > 2009))\n",
    ")\n",
    "final_crit_a2.equals(final_crit_a)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Comparing Row Filtering and Index Filtering"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### How to do it..."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 45,
   "metadata": {
    "lines_to_next_cell": 2
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>INSTNM</th>\n",
       "      <th>CITY</th>\n",
       "      <th>...</th>\n",
       "      <th>MD_EARN_WNE_P10</th>\n",
       "      <th>GRAD_DEBT_MDN_SUPP</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>3610</th>\n",
       "      <td>Abilene ...</td>\n",
       "      <td>Abilene</td>\n",
       "      <td>...</td>\n",
       "      <td>40200</td>\n",
       "      <td>25985</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3611</th>\n",
       "      <td>Alvin Co...</td>\n",
       "      <td>Alvin</td>\n",
       "      <td>...</td>\n",
       "      <td>34500</td>\n",
       "      <td>6750</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3612</th>\n",
       "      <td>Amarillo...</td>\n",
       "      <td>Amarillo</td>\n",
       "      <td>...</td>\n",
       "      <td>31700</td>\n",
       "      <td>10950</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3613</th>\n",
       "      <td>Angelina...</td>\n",
       "      <td>Lufkin</td>\n",
       "      <td>...</td>\n",
       "      <td>26900</td>\n",
       "      <td>PrivacyS...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3614</th>\n",
       "      <td>Angelo S...</td>\n",
       "      <td>San Angelo</td>\n",
       "      <td>...</td>\n",
       "      <td>37700</td>\n",
       "      <td>21319.5</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>5 rows × 27 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "           INSTNM        CITY  ... MD_EARN_WNE_P10  GRAD_DEBT_MDN_SUPP\n",
       "3610  Abilene ...     Abilene  ...        40200           25985       \n",
       "3611  Alvin Co...       Alvin  ...        34500            6750       \n",
       "3612  Amarillo...    Amarillo  ...        31700           10950       \n",
       "3613  Angelina...      Lufkin  ...        26900     PrivacyS...       \n",
       "3614  Angelo S...  San Angelo  ...        37700         21319.5       \n",
       "\n",
       "[5 rows x 27 columns]"
      ]
     },
     "execution_count": 45,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "college = pd.read_csv('data/college.csv')\n",
    "college[college['STABBR'] == 'TX'].head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 46,
   "metadata": {
    "lines_to_next_cell": 2
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>INSTNM</th>\n",
       "      <th>CITY</th>\n",
       "      <th>...</th>\n",
       "      <th>MD_EARN_WNE_P10</th>\n",
       "      <th>GRAD_DEBT_MDN_SUPP</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>STABBR</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>TX</th>\n",
       "      <td>Abilene ...</td>\n",
       "      <td>Abilene</td>\n",
       "      <td>...</td>\n",
       "      <td>40200</td>\n",
       "      <td>25985</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>TX</th>\n",
       "      <td>Alvin Co...</td>\n",
       "      <td>Alvin</td>\n",
       "      <td>...</td>\n",
       "      <td>34500</td>\n",
       "      <td>6750</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>TX</th>\n",
       "      <td>Amarillo...</td>\n",
       "      <td>Amarillo</td>\n",
       "      <td>...</td>\n",
       "      <td>31700</td>\n",
       "      <td>10950</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>TX</th>\n",
       "      <td>Angelina...</td>\n",
       "      <td>Lufkin</td>\n",
       "      <td>...</td>\n",
       "      <td>26900</td>\n",
       "      <td>PrivacyS...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>TX</th>\n",
       "      <td>Angelo S...</td>\n",
       "      <td>San Angelo</td>\n",
       "      <td>...</td>\n",
       "      <td>37700</td>\n",
       "      <td>21319.5</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>5 rows × 26 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "             INSTNM        CITY  ...  MD_EARN_WNE_P10  GRAD_DEBT_MDN_SUPP\n",
       "STABBR                           ...                                     \n",
       "TX      Abilene ...     Abilene  ...        40200            25985       \n",
       "TX      Alvin Co...       Alvin  ...        34500             6750       \n",
       "TX      Amarillo...    Amarillo  ...        31700            10950       \n",
       "TX      Angelina...      Lufkin  ...        26900      PrivacyS...       \n",
       "TX      Angelo S...  San Angelo  ...        37700          21319.5       \n",
       "\n",
       "[5 rows x 26 columns]"
      ]
     },
     "execution_count": 46,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "college2 = college.set_index('STABBR')\n",
    "college2.loc['TX'].head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 47,
   "metadata": {
    "lines_to_next_cell": 2
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2.02 ms ± 427 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)\n"
     ]
    }
   ],
   "source": [
    "%timeit college[college['STABBR'] == 'TX']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 48,
   "metadata": {
    "lines_to_next_cell": 2
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "798 µs ± 49.4 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)\n"
     ]
    }
   ],
   "source": [
    "%timeit college2.loc['TX']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 49,
   "metadata": {
    "lines_to_next_cell": 2
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1.93 ms ± 111 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)\n"
     ]
    }
   ],
   "source": [
    "%timeit college2 = college.set_index('STABBR')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### How it works..."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### There's more..."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 50,
   "metadata": {
    "lines_to_next_cell": 2
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>INSTNM</th>\n",
       "      <th>CITY</th>\n",
       "      <th>...</th>\n",
       "      <th>MD_EARN_WNE_P10</th>\n",
       "      <th>GRAD_DEBT_MDN_SUPP</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>192</th>\n",
       "      <td>Academy ...</td>\n",
       "      <td>San Fran...</td>\n",
       "      <td>...</td>\n",
       "      <td>36000</td>\n",
       "      <td>35093</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>193</th>\n",
       "      <td>ITT Tech...</td>\n",
       "      <td>Rancho C...</td>\n",
       "      <td>...</td>\n",
       "      <td>38800</td>\n",
       "      <td>25827.5</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>194</th>\n",
       "      <td>Academy ...</td>\n",
       "      <td>Oakland</td>\n",
       "      <td>...</td>\n",
       "      <td>NaN</td>\n",
       "      <td>PrivacyS...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>195</th>\n",
       "      <td>The Acad...</td>\n",
       "      <td>Huntingt...</td>\n",
       "      <td>...</td>\n",
       "      <td>28400</td>\n",
       "      <td>9500</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>196</th>\n",
       "      <td>Avalon S...</td>\n",
       "      <td>Alameda</td>\n",
       "      <td>...</td>\n",
       "      <td>21600</td>\n",
       "      <td>9860</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7528</th>\n",
       "      <td>WestMed ...</td>\n",
       "      <td>Merced</td>\n",
       "      <td>...</td>\n",
       "      <td>NaN</td>\n",
       "      <td>15623.5</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7529</th>\n",
       "      <td>Vantage ...</td>\n",
       "      <td>El Paso</td>\n",
       "      <td>...</td>\n",
       "      <td>NaN</td>\n",
       "      <td>9500</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7530</th>\n",
       "      <td>SAE Inst...</td>\n",
       "      <td>Emeryville</td>\n",
       "      <td>...</td>\n",
       "      <td>NaN</td>\n",
       "      <td>9500</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7533</th>\n",
       "      <td>Bay Area...</td>\n",
       "      <td>San Jose</td>\n",
       "      <td>...</td>\n",
       "      <td>NaN</td>\n",
       "      <td>PrivacyS...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7534</th>\n",
       "      <td>Excel Le...</td>\n",
       "      <td>San Antonio</td>\n",
       "      <td>...</td>\n",
       "      <td>NaN</td>\n",
       "      <td>12125</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>1704 rows × 27 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "           INSTNM         CITY  ... MD_EARN_WNE_P10  GRAD_DEBT_MDN_SUPP\n",
       "192   Academy ...  San Fran...  ...        36000           35093       \n",
       "193   ITT Tech...  Rancho C...  ...        38800         25827.5       \n",
       "194   Academy ...      Oakland  ...          NaN     PrivacyS...       \n",
       "195   The Acad...  Huntingt...  ...        28400            9500       \n",
       "196   Avalon S...      Alameda  ...        21600            9860       \n",
       "...           ...          ...  ...          ...             ...       \n",
       "7528  WestMed ...       Merced  ...          NaN         15623.5       \n",
       "7529  Vantage ...      El Paso  ...          NaN            9500       \n",
       "7530  SAE Inst...   Emeryville  ...          NaN            9500       \n",
       "7533  Bay Area...     San Jose  ...          NaN     PrivacyS...       \n",
       "7534  Excel Le...  San Antonio  ...          NaN           12125       \n",
       "\n",
       "[1704 rows x 27 columns]"
      ]
     },
     "execution_count": 50,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "states = ['TX', 'CA', 'NY']\n",
    "college[college['STABBR'].isin(states)]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 51,
   "metadata": {
    "lines_to_next_cell": 2
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>INSTNM</th>\n",
       "      <th>CITY</th>\n",
       "      <th>...</th>\n",
       "      <th>MD_EARN_WNE_P10</th>\n",
       "      <th>GRAD_DEBT_MDN_SUPP</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>STABBR</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>TX</th>\n",
       "      <td>Abilene ...</td>\n",
       "      <td>Abilene</td>\n",
       "      <td>...</td>\n",
       "      <td>40200</td>\n",
       "      <td>25985</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>TX</th>\n",
       "      <td>Alvin Co...</td>\n",
       "      <td>Alvin</td>\n",
       "      <td>...</td>\n",
       "      <td>34500</td>\n",
       "      <td>6750</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>TX</th>\n",
       "      <td>Amarillo...</td>\n",
       "      <td>Amarillo</td>\n",
       "      <td>...</td>\n",
       "      <td>31700</td>\n",
       "      <td>10950</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>TX</th>\n",
       "      <td>Angelina...</td>\n",
       "      <td>Lufkin</td>\n",
       "      <td>...</td>\n",
       "      <td>26900</td>\n",
       "      <td>PrivacyS...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>TX</th>\n",
       "      <td>Angelo S...</td>\n",
       "      <td>San Angelo</td>\n",
       "      <td>...</td>\n",
       "      <td>37700</td>\n",
       "      <td>21319.5</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>NY</th>\n",
       "      <td>Briarcli...</td>\n",
       "      <td>Patchogue</td>\n",
       "      <td>...</td>\n",
       "      <td>38200</td>\n",
       "      <td>28720.5</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>NY</th>\n",
       "      <td>Jamestow...</td>\n",
       "      <td>Salamanca</td>\n",
       "      <td>...</td>\n",
       "      <td>NaN</td>\n",
       "      <td>12050</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>NY</th>\n",
       "      <td>Pratt Ma...</td>\n",
       "      <td>New York</td>\n",
       "      <td>...</td>\n",
       "      <td>40900</td>\n",
       "      <td>26691</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>NY</th>\n",
       "      <td>Saint Jo...</td>\n",
       "      <td>Patchogue</td>\n",
       "      <td>...</td>\n",
       "      <td>52000</td>\n",
       "      <td>22143.5</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>NY</th>\n",
       "      <td>Franklin...</td>\n",
       "      <td>Brooklyn</td>\n",
       "      <td>...</td>\n",
       "      <td>20000</td>\n",
       "      <td>PrivacyS...</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>1704 rows × 26 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "             INSTNM        CITY  ...  MD_EARN_WNE_P10  GRAD_DEBT_MDN_SUPP\n",
       "STABBR                           ...                                     \n",
       "TX      Abilene ...     Abilene  ...        40200            25985       \n",
       "TX      Alvin Co...       Alvin  ...        34500             6750       \n",
       "TX      Amarillo...    Amarillo  ...        31700            10950       \n",
       "TX      Angelina...      Lufkin  ...        26900      PrivacyS...       \n",
       "TX      Angelo S...  San Angelo  ...        37700          21319.5       \n",
       "...             ...         ...  ...          ...              ...       \n",
       "NY      Briarcli...   Patchogue  ...        38200          28720.5       \n",
       "NY      Jamestow...   Salamanca  ...          NaN            12050       \n",
       "NY      Pratt Ma...    New York  ...        40900            26691       \n",
       "NY      Saint Jo...   Patchogue  ...        52000          22143.5       \n",
       "NY      Franklin...    Brooklyn  ...        20000      PrivacyS...       \n",
       "\n",
       "[1704 rows x 26 columns]"
      ]
     },
     "execution_count": 51,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "college2.loc[states]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Selecting with unique and sorted indexes"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### How to do it..."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 52,
   "metadata": {
    "lines_to_next_cell": 2
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "False"
      ]
     },
     "execution_count": 52,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "college = pd.read_csv('data/college.csv')\n",
    "college2 = college.set_index('STABBR')\n",
    "college2.index.is_monotonic"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 53,
   "metadata": {
    "lines_to_next_cell": 2
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 53,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "college3 = college2.sort_index()\n",
    "college3.index.is_monotonic"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 54,
   "metadata": {
    "lines_to_next_cell": 2
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1.61 ms ± 79.2 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)\n"
     ]
    }
   ],
   "source": [
    "%timeit college[college['STABBR'] == 'TX']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 55,
   "metadata": {
    "lines_to_next_cell": 2
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "789 µs ± 25.2 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)\n"
     ]
    }
   ],
   "source": [
    "%timeit college2.loc['TX']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 56,
   "metadata": {
    "lines_to_next_cell": 2
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "272 µs ± 7.09 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)\n"
     ]
    }
   ],
   "source": [
    "%timeit college3.loc['TX']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 57,
   "metadata": {
    "lines_to_next_cell": 2
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 57,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "college_unique = college.set_index('INSTNM')\n",
    "college_unique.index.is_unique"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 58,
   "metadata": {
    "lines_to_next_cell": 2
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>INSTNM</th>\n",
       "      <th>CITY</th>\n",
       "      <th>...</th>\n",
       "      <th>MD_EARN_WNE_P10</th>\n",
       "      <th>GRAD_DEBT_MDN_SUPP</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>4217</th>\n",
       "      <td>Stanford...</td>\n",
       "      <td>Stanford</td>\n",
       "      <td>...</td>\n",
       "      <td>86000</td>\n",
       "      <td>12782</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>1 rows × 27 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "           INSTNM      CITY  ... MD_EARN_WNE_P10  GRAD_DEBT_MDN_SUPP\n",
       "4217  Stanford...  Stanford  ...        86000           12782       \n",
       "\n",
       "[1 rows x 27 columns]"
      ]
     },
     "execution_count": 58,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "college[college['INSTNM'] == 'Stanford University']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 59,
   "metadata": {
    "lines_to_next_cell": 2
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "CITY                  Stanford\n",
       "STABBR                      CA\n",
       "HBCU                         0\n",
       "MENONLY                      0\n",
       "WOMENONLY                    0\n",
       "                        ...   \n",
       "PCTPELL                 0.1556\n",
       "PCTFLOAN                0.1256\n",
       "UG25ABV                 0.0401\n",
       "MD_EARN_WNE_P10          86000\n",
       "GRAD_DEBT_MDN_SUPP       12782\n",
       "Name: Stanford University, Length: 26, dtype: object"
      ]
     },
     "execution_count": 59,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "college_unique.loc['Stanford University']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 60,
   "metadata": {
    "lines_to_next_cell": 2
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>CITY</th>\n",
       "      <th>STABBR</th>\n",
       "      <th>...</th>\n",
       "      <th>MD_EARN_WNE_P10</th>\n",
       "      <th>GRAD_DEBT_MDN_SUPP</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>INSTNM</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>Stanford University</th>\n",
       "      <td>Stanford</td>\n",
       "      <td>CA</td>\n",
       "      <td>...</td>\n",
       "      <td>86000</td>\n",
       "      <td>12782</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>1 rows × 26 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "                  CITY STABBR  ...  MD_EARN_WNE_P10  GRAD_DEBT_MDN_SUPP\n",
       "INSTNM                         ...                                     \n",
       "Stanford ...  Stanford     CA  ...        86000            12782       \n",
       "\n",
       "[1 rows x 26 columns]"
      ]
     },
     "execution_count": 60,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "college_unique.loc[['Stanford University']]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 61,
   "metadata": {
    "lines_to_next_cell": 2
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1.42 ms ± 48.3 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)\n"
     ]
    }
   ],
   "source": [
    "%timeit college[college['INSTNM'] == 'Stanford University']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 62,
   "metadata": {
    "lines_to_next_cell": 2
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "848 µs ± 47.5 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)\n"
     ]
    }
   ],
   "source": [
    "%timeit college_unique.loc[['Stanford University']]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### How it works..."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### There's more..."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 63,
   "metadata": {
    "lines_to_next_cell": 2
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>INSTNM</th>\n",
       "      <th>CITY</th>\n",
       "      <th>...</th>\n",
       "      <th>MD_EARN_WNE_P10</th>\n",
       "      <th>GRAD_DEBT_MDN_SUPP</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>ARTESIA, CA</th>\n",
       "      <td>Angeles ...</td>\n",
       "      <td>ARTESIA</td>\n",
       "      <td>...</td>\n",
       "      <td>NaN</td>\n",
       "      <td>16850</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Aberdeen, SD</th>\n",
       "      <td>Presenta...</td>\n",
       "      <td>Aberdeen</td>\n",
       "      <td>...</td>\n",
       "      <td>35900</td>\n",
       "      <td>25000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Aberdeen, SD</th>\n",
       "      <td>Northern...</td>\n",
       "      <td>Aberdeen</td>\n",
       "      <td>...</td>\n",
       "      <td>33600</td>\n",
       "      <td>24847</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Aberdeen, WA</th>\n",
       "      <td>Grays Ha...</td>\n",
       "      <td>Aberdeen</td>\n",
       "      <td>...</td>\n",
       "      <td>27000</td>\n",
       "      <td>11490</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Abilene, TX</th>\n",
       "      <td>Hardin-S...</td>\n",
       "      <td>Abilene</td>\n",
       "      <td>...</td>\n",
       "      <td>38700</td>\n",
       "      <td>25864</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>5 rows × 27 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "                   INSTNM      CITY  ... MD_EARN_WNE_P10  GRAD_DEBT_MDN_SUPP\n",
       "ARTESIA, CA   Angeles ...   ARTESIA  ...          NaN           16850       \n",
       "Aberdeen, SD  Presenta...  Aberdeen  ...        35900           25000       \n",
       "Aberdeen, SD  Northern...  Aberdeen  ...        33600           24847       \n",
       "Aberdeen, WA  Grays Ha...  Aberdeen  ...        27000           11490       \n",
       "Abilene, TX   Hardin-S...   Abilene  ...        38700           25864       \n",
       "\n",
       "[5 rows x 27 columns]"
      ]
     },
     "execution_count": 63,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "college.index = college['CITY'] + ', ' + college['STABBR']\n",
    "college = college.sort_index()\n",
    "college.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 64,
   "metadata": {
    "lines_to_next_cell": 2
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>INSTNM</th>\n",
       "      <th>CITY</th>\n",
       "      <th>...</th>\n",
       "      <th>MD_EARN_WNE_P10</th>\n",
       "      <th>GRAD_DEBT_MDN_SUPP</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>Miami, FL</th>\n",
       "      <td>New Prof...</td>\n",
       "      <td>Miami</td>\n",
       "      <td>...</td>\n",
       "      <td>18700</td>\n",
       "      <td>8682</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Miami, FL</th>\n",
       "      <td>Manageme...</td>\n",
       "      <td>Miami</td>\n",
       "      <td>...</td>\n",
       "      <td>PrivacyS...</td>\n",
       "      <td>12182</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Miami, FL</th>\n",
       "      <td>Strayer ...</td>\n",
       "      <td>Miami</td>\n",
       "      <td>...</td>\n",
       "      <td>49200</td>\n",
       "      <td>36173.5</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Miami, FL</th>\n",
       "      <td>Keiser U...</td>\n",
       "      <td>Miami</td>\n",
       "      <td>...</td>\n",
       "      <td>29700</td>\n",
       "      <td>26063</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Miami, FL</th>\n",
       "      <td>George T...</td>\n",
       "      <td>Miami</td>\n",
       "      <td>...</td>\n",
       "      <td>38600</td>\n",
       "      <td>PrivacyS...</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>5 rows × 27 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "                INSTNM   CITY  ... MD_EARN_WNE_P10  GRAD_DEBT_MDN_SUPP\n",
       "Miami, FL  New Prof...  Miami  ...        18700            8682       \n",
       "Miami, FL  Manageme...  Miami  ...  PrivacyS...           12182       \n",
       "Miami, FL  Strayer ...  Miami  ...        49200         36173.5       \n",
       "Miami, FL  Keiser U...  Miami  ...        29700           26063       \n",
       "Miami, FL  George T...  Miami  ...        38600     PrivacyS...       \n",
       "\n",
       "[5 rows x 27 columns]"
      ]
     },
     "execution_count": 64,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "college.loc['Miami, FL'].head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 65,
   "metadata": {
    "lines_to_next_cell": 2
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "3.6 ms ± 635 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)\n"
     ]
    }
   ],
   "source": [
    "%%timeit\n",
    "crit1 = college['CITY'] == 'Miami'\n",
    "crit2 = college['STABBR'] == 'FL'\n",
    "college[crit1 & crit2]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 66,
   "metadata": {
    "lines_to_next_cell": 2
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "305 µs ± 14 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)\n"
     ]
    }
   ],
   "source": [
    "%timeit college.loc['Miami, FL']"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Translating SQL WHERE clauses"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### How to do it..."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 67,
   "metadata": {
    "lines_to_next_cell": 2
   },
   "outputs": [],
   "source": [
    "employee = pd.read_csv('data/employee.csv')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 68,
   "metadata": {
    "lines_to_next_cell": 2
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "UNIQUE_ID              int64\n",
       "POSITION_TITLE        object\n",
       "DEPARTMENT            object\n",
       "BASE_SALARY          float64\n",
       "RACE                  object\n",
       "EMPLOYMENT_TYPE       object\n",
       "GENDER                object\n",
       "EMPLOYMENT_STATUS     object\n",
       "HIRE_DATE             object\n",
       "JOB_DATE              object\n",
       "dtype: object"
      ]
     },
     "execution_count": 68,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "employee.dtypes"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 69,
   "metadata": {
    "lines_to_next_cell": 2
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Houston Police Department-HPD     638\n",
       "Houston Fire Department (HFD)     384\n",
       "Public Works & Engineering-PWE    343\n",
       "Health & Human Services           110\n",
       "Houston Airport System (HAS)      106\n",
       "Name: DEPARTMENT, dtype: int64"
      ]
     },
     "execution_count": 69,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "employee.DEPARTMENT.value_counts().head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 70,
   "metadata": {
    "lines_to_next_cell": 2
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "Male      1397\n",
       "Female     603\n",
       "Name: GENDER, dtype: int64"
      ]
     },
     "execution_count": 70,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "employee.GENDER.value_counts()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 71,
   "metadata": {
    "lines_to_next_cell": 2
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "count    1886.000000\n",
       "mean     55767.93...\n",
       "std      21693.70...\n",
       "min      24960.00...\n",
       "25%      40170.00...\n",
       "50%      54461.00...\n",
       "75%      66614.00...\n",
       "max      275000.0...\n",
       "Name: BASE_SALARY, dtype: float64"
      ]
     },
     "execution_count": 71,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "employee.BASE_SALARY.describe()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 72,
   "metadata": {
    "lines_to_next_cell": 2
   },
   "outputs": [],
   "source": [
    "depts = ['Houston Police Department-HPD',\n",
    "   'Houston Fire Department (HFD)']\n",
    "criteria_dept = employee.DEPARTMENT.isin(depts)\n",
    "criteria_gender = employee.GENDER == 'Female'\n",
    "criteria_sal = ((employee.BASE_SALARY >= 80000) & \n",
    "   (employee.BASE_SALARY <= 120000))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 73,
   "metadata": {
    "lines_to_next_cell": 2
   },
   "outputs": [],
   "source": [
    "criteria_final = (criteria_dept &\n",
    "   criteria_gender &\n",
    "   criteria_sal)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 74,
   "metadata": {
    "lines_to_next_cell": 2
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>UNIQUE_ID</th>\n",
       "      <th>DEPARTMENT</th>\n",
       "      <th>GENDER</th>\n",
       "      <th>BASE_SALARY</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>61</th>\n",
       "      <td>61</td>\n",
       "      <td>Houston ...</td>\n",
       "      <td>Female</td>\n",
       "      <td>96668.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>136</th>\n",
       "      <td>136</td>\n",
       "      <td>Houston ...</td>\n",
       "      <td>Female</td>\n",
       "      <td>81239.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>367</th>\n",
       "      <td>367</td>\n",
       "      <td>Houston ...</td>\n",
       "      <td>Female</td>\n",
       "      <td>86534.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>474</th>\n",
       "      <td>474</td>\n",
       "      <td>Houston ...</td>\n",
       "      <td>Female</td>\n",
       "      <td>91181.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>513</th>\n",
       "      <td>513</td>\n",
       "      <td>Houston ...</td>\n",
       "      <td>Female</td>\n",
       "      <td>81239.0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "     UNIQUE_ID   DEPARTMENT  GENDER  BASE_SALARY\n",
       "61          61  Houston ...  Female      96668.0\n",
       "136        136  Houston ...  Female      81239.0\n",
       "367        367  Houston ...  Female      86534.0\n",
       "474        474  Houston ...  Female      91181.0\n",
       "513        513  Houston ...  Female      81239.0"
      ]
     },
     "execution_count": 74,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "select_columns = ['UNIQUE_ID', 'DEPARTMENT',\n",
    "                  'GENDER', 'BASE_SALARY']\n",
    "employee.loc[criteria_final, select_columns].head()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### How it works..."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### There's more..."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 75,
   "metadata": {
    "lines_to_next_cell": 2
   },
   "outputs": [],
   "source": [
    "criteria_sal = employee.BASE_SALARY.between(80_000, 120_000)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 76,
   "metadata": {
    "lines_to_next_cell": 2
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>UNIQUE_ID</th>\n",
       "      <th>POSITION_TITLE</th>\n",
       "      <th>...</th>\n",
       "      <th>HIRE_DATE</th>\n",
       "      <th>JOB_DATE</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>0</td>\n",
       "      <td>ASSISTAN...</td>\n",
       "      <td>...</td>\n",
       "      <td>2006-06-12</td>\n",
       "      <td>2012-10-13</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>1</td>\n",
       "      <td>LIBRARY ...</td>\n",
       "      <td>...</td>\n",
       "      <td>2000-07-19</td>\n",
       "      <td>2010-09-18</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>4</td>\n",
       "      <td>ELECTRICIAN</td>\n",
       "      <td>...</td>\n",
       "      <td>1989-06-19</td>\n",
       "      <td>1994-10-22</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>18</th>\n",
       "      <td>18</td>\n",
       "      <td>MAINTENA...</td>\n",
       "      <td>...</td>\n",
       "      <td>2008-12-29</td>\n",
       "      <td>2008-12-29</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>32</th>\n",
       "      <td>32</td>\n",
       "      <td>SENIOR A...</td>\n",
       "      <td>...</td>\n",
       "      <td>1991-02-11</td>\n",
       "      <td>2016-02-13</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1976</th>\n",
       "      <td>1976</td>\n",
       "      <td>SENIOR S...</td>\n",
       "      <td>...</td>\n",
       "      <td>2015-07-20</td>\n",
       "      <td>2016-01-30</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1983</th>\n",
       "      <td>1983</td>\n",
       "      <td>ADMINIST...</td>\n",
       "      <td>...</td>\n",
       "      <td>2006-10-16</td>\n",
       "      <td>2006-10-16</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1985</th>\n",
       "      <td>1985</td>\n",
       "      <td>TRUCK DR...</td>\n",
       "      <td>...</td>\n",
       "      <td>2013-06-10</td>\n",
       "      <td>2015-08-01</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1988</th>\n",
       "      <td>1988</td>\n",
       "      <td>SENIOR A...</td>\n",
       "      <td>...</td>\n",
       "      <td>2013-01-23</td>\n",
       "      <td>2013-03-02</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1990</th>\n",
       "      <td>1990</td>\n",
       "      <td>BUILDING...</td>\n",
       "      <td>...</td>\n",
       "      <td>1995-10-14</td>\n",
       "      <td>2010-03-20</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>419 rows × 10 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "      UNIQUE_ID POSITION_TITLE  ...   HIRE_DATE    JOB_DATE\n",
       "0             0  ASSISTAN...    ...  2006-06-12  2012-10-13\n",
       "1             1  LIBRARY ...    ...  2000-07-19  2010-09-18\n",
       "4             4  ELECTRICIAN    ...  1989-06-19  1994-10-22\n",
       "18           18  MAINTENA...    ...  2008-12-29  2008-12-29\n",
       "32           32  SENIOR A...    ...  1991-02-11  2016-02-13\n",
       "...         ...          ...    ...         ...         ...\n",
       "1976       1976  SENIOR S...    ...  2015-07-20  2016-01-30\n",
       "1983       1983  ADMINIST...    ...  2006-10-16  2006-10-16\n",
       "1985       1985  TRUCK DR...    ...  2013-06-10  2015-08-01\n",
       "1988       1988  SENIOR A...    ...  2013-01-23  2013-03-02\n",
       "1990       1990  BUILDING...    ...  1995-10-14  2010-03-20\n",
       "\n",
       "[419 rows x 10 columns]"
      ]
     },
     "execution_count": 76,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "top_5_depts = employee.DEPARTMENT.value_counts().index[:5]\n",
    "criteria = ~employee.DEPARTMENT.isin(top_5_depts)\n",
    "employee[criteria]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Improving readability of boolean indexing with the query method"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### How to do it..."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 77,
   "metadata": {
    "lines_to_next_cell": 2
   },
   "outputs": [],
   "source": [
    "employee = pd.read_csv('data/employee.csv')\n",
    "depts = ['Houston Police Department-HPD',\n",
    "         'Houston Fire Department (HFD)']\n",
    "select_columns = ['UNIQUE_ID', 'DEPARTMENT',\n",
    "                  'GENDER', 'BASE_SALARY']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 78,
   "metadata": {
    "lines_to_next_cell": 2
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>UNIQUE_ID</th>\n",
       "      <th>DEPARTMENT</th>\n",
       "      <th>GENDER</th>\n",
       "      <th>BASE_SALARY</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>61</th>\n",
       "      <td>61</td>\n",
       "      <td>Houston ...</td>\n",
       "      <td>Female</td>\n",
       "      <td>96668.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>136</th>\n",
       "      <td>136</td>\n",
       "      <td>Houston ...</td>\n",
       "      <td>Female</td>\n",
       "      <td>81239.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>367</th>\n",
       "      <td>367</td>\n",
       "      <td>Houston ...</td>\n",
       "      <td>Female</td>\n",
       "      <td>86534.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>474</th>\n",
       "      <td>474</td>\n",
       "      <td>Houston ...</td>\n",
       "      <td>Female</td>\n",
       "      <td>91181.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>513</th>\n",
       "      <td>513</td>\n",
       "      <td>Houston ...</td>\n",
       "      <td>Female</td>\n",
       "      <td>81239.0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "     UNIQUE_ID   DEPARTMENT  GENDER  BASE_SALARY\n",
       "61          61  Houston ...  Female      96668.0\n",
       "136        136  Houston ...  Female      81239.0\n",
       "367        367  Houston ...  Female      86534.0\n",
       "474        474  Houston ...  Female      91181.0\n",
       "513        513  Houston ...  Female      81239.0"
      ]
     },
     "execution_count": 78,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "qs = \"DEPARTMENT in @depts \"\\\n",
    "     \" and GENDER == 'Female' \"\\\n",
    "     \" and 80000 <= BASE_SALARY <= 120000\"\n",
    "emp_filtered = employee.query(qs)\n",
    "emp_filtered[select_columns].head()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### How it works..."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### There's more..."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 79,
   "metadata": {
    "lines_to_next_cell": 2
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>UNIQUE_ID</th>\n",
       "      <th>POSITION_TITLE</th>\n",
       "      <th>...</th>\n",
       "      <th>HIRE_DATE</th>\n",
       "      <th>JOB_DATE</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>0</td>\n",
       "      <td>ASSISTAN...</td>\n",
       "      <td>...</td>\n",
       "      <td>2006-06-12</td>\n",
       "      <td>2012-10-13</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>73</th>\n",
       "      <td>73</td>\n",
       "      <td>ADMINIST...</td>\n",
       "      <td>...</td>\n",
       "      <td>2011-12-19</td>\n",
       "      <td>2013-11-23</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>96</th>\n",
       "      <td>96</td>\n",
       "      <td>ASSISTAN...</td>\n",
       "      <td>...</td>\n",
       "      <td>2013-06-10</td>\n",
       "      <td>2013-06-10</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>117</th>\n",
       "      <td>117</td>\n",
       "      <td>SENIOR A...</td>\n",
       "      <td>...</td>\n",
       "      <td>1998-03-20</td>\n",
       "      <td>2012-07-21</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>146</th>\n",
       "      <td>146</td>\n",
       "      <td>SENIOR S...</td>\n",
       "      <td>...</td>\n",
       "      <td>2014-03-17</td>\n",
       "      <td>2014-03-17</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>5 rows × 10 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "     UNIQUE_ID POSITION_TITLE  ...   HIRE_DATE    JOB_DATE\n",
       "0            0  ASSISTAN...    ...  2006-06-12  2012-10-13\n",
       "73          73  ADMINIST...    ...  2011-12-19  2013-11-23\n",
       "96          96  ASSISTAN...    ...  2013-06-10  2013-06-10\n",
       "117        117  SENIOR A...    ...  1998-03-20  2012-07-21\n",
       "146        146  SENIOR S...    ...  2014-03-17  2014-03-17\n",
       "\n",
       "[5 rows x 10 columns]"
      ]
     },
     "execution_count": 79,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "top10_depts = (employee.DEPARTMENT.value_counts() \n",
    "   .index[:10].tolist()\n",
    ")\n",
    "qs = \"DEPARTMENT not in @top10_depts and GENDER == 'Female'\"\n",
    "employee_filtered2 = employee.query(qs)\n",
    "employee_filtered2.head()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Preserving Series size with the where method"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### How to do it..."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 80,
   "metadata": {
    "lines_to_next_cell": 2
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "movie_title\n",
       "Avatar                                         1000.0\n",
       "Pirates of the Caribbean: At World's End      40000.0\n",
       "Spectre                                       11000.0\n",
       "The Dark Knight Rises                         27000.0\n",
       "Star Wars: Episode VII - The Force Awakens      131.0\n",
       "Name: actor_1_facebook_likes, dtype: float64"
      ]
     },
     "execution_count": 80,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "movie = pd.read_csv('data/movie.csv', index_col='movie_title')\n",
    "fb_likes = movie['actor_1_facebook_likes'].dropna()\n",
    "fb_likes.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 81,
   "metadata": {
    "lines_to_next_cell": 2
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "count    4909.000000\n",
       "mean     6494.488491\n",
       "std      15106.98...\n",
       "min         0.000000\n",
       "25%       607.000000\n",
       "50%       982.000000\n",
       "75%      11000.00...\n",
       "max      640000.0...\n",
       "Name: actor_1_facebook_likes, dtype: float64"
      ]
     },
     "execution_count": 81,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "fb_likes.describe()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 82,
   "metadata": {
    "lines_to_next_cell": 2
   },
   "outputs": [],
   "source": [
    "import matplotlib.pyplot as plt\n",
    "fig, ax = plt.subplots(figsize=(10, 8))\n",
    "fb_likes.hist(ax=ax)\n",
    "fig.savefig('/tmp/c7-hist.png', dpi=300)     # doctest: +SKIP"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 83,
   "metadata": {
    "lines_to_next_cell": 2
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.91"
      ]
     },
     "execution_count": 83,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "criteria_high = fb_likes < 20_000\n",
    "criteria_high.mean().round(2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 84,
   "metadata": {
    "lines_to_next_cell": 2
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "movie_title\n",
       "Avatar                                         1000.0\n",
       "Pirates of the Caribbean: At World's End          NaN\n",
       "Spectre                                       11000.0\n",
       "The Dark Knight Rises                             NaN\n",
       "Star Wars: Episode VII - The Force Awakens      131.0\n",
       "Name: actor_1_facebook_likes, dtype: float64"
      ]
     },
     "execution_count": 84,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "fb_likes.where(criteria_high).head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 85,
   "metadata": {
    "lines_to_next_cell": 2
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "movie_title\n",
       "Avatar                                         1000.0\n",
       "Pirates of the Caribbean: At World's End      20000.0\n",
       "Spectre                                       11000.0\n",
       "The Dark Knight Rises                         20000.0\n",
       "Star Wars: Episode VII - The Force Awakens      131.0\n",
       "Name: actor_1_facebook_likes, dtype: float64"
      ]
     },
     "execution_count": 85,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "fb_likes.where(criteria_high, other=20000).head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 86,
   "metadata": {
    "lines_to_next_cell": 2
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "movie_title\n",
       "Avatar                                         1000.0\n",
       "Pirates of the Caribbean: At World's End      20000.0\n",
       "Spectre                                       11000.0\n",
       "The Dark Knight Rises                         20000.0\n",
       "Star Wars: Episode VII - The Force Awakens      300.0\n",
       "Name: actor_1_facebook_likes, dtype: float64"
      ]
     },
     "execution_count": 86,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "criteria_low = fb_likes > 300\n",
    "fb_likes_cap = (fb_likes\n",
    "   .where(criteria_high, other=20_000)\n",
    "   .where(criteria_low, 300)\n",
    ")\n",
    "fb_likes_cap.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 87,
   "metadata": {
    "lines_to_next_cell": 2
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(4909, 4909)"
      ]
     },
     "execution_count": 87,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "len(fb_likes), len(fb_likes_cap)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 88,
   "metadata": {
    "lines_to_next_cell": 2
   },
   "outputs": [
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAmAAAAHVCAYAAABWhEeLAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADl0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uIDIuMi40LCBodHRwOi8vbWF0cGxvdGxpYi5vcmcv7US4rQAAHLlJREFUeJzt3X2sZGd9H/DvLzaQyktsU+jKtd2s0zipHFCIWYGrvOhuaOy108akTZERAhuInFa2FBSqZkmUQkOQnOYFCYWQbmQLk5AszgtiZZsSx2WL+MOATRz8FscLmJaVYyvYmGxIaU1//WPORsN6797rvdfP3nvn85FG98xznjnn/J6ZnfnueZmp7g4AAON8y8neAACARSOAAQAMJoABAAwmgAEADCaAAQAMJoABAAwmgAEADCaAAQAMJoABAAx26snegON54Qtf2Dt27Fjzcv72b/82p5122to3aJNa9PoTY7Do9SfGQP2LXX9iDEbUf9ddd/11d79oNX03dADbsWNH7rzzzjUv58CBA1laWlr7Bm1Si15/YgwW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NkhqlOPat/wpm3+10k+eKRtq74GjvXZl034PrDVA9iW/emj6Vj/9Uke6O5fn2s/a67bjyc5cqXM/iRXVNXzquq8JOdndqLhphyjqjqtqp5/ZDqzE5HvzWzbj1zNcmWSD0/T+5O8froi5qIkT067qz+a5OKqOnM6bHHx1LZZfNP/eBfl+T/Kujzn07yvVtVF07+v188ta8Oqqt1J/mOSH+vur821v6iqTpmmvyOz5/zzK9S53FhuaOv1up/C68eS/MT0+E0zBkn+RZK/6O6/P3y2FV8Dy332ZTO+DzyTM/Y34y2zKyD+MrPk//Mne3vWsa4fyGwX62eT3D3dLkvyO0numdr3Jzlr7jE/P43Dg5m7qmMzjlFmVy/9+XS778h2Z3YOx+1JHkryp0leMLVXkvdMNd6TZOfcst6Y2cm5B5O84WTX9gzG4LTM/sd++lzbln7+MwubjyT5v5mdm/Gm9XzOk+zM7MP7c0l+I9OvhWyU2zL1H8zsXJYj7wO/NfX9N9O/jbuTfCbJv1qpzuXGciPdlhmDdXvdT+8tn5rG9Q+SPO9k17xS/VP7+5L8u6P6brnXQJb/7Nt07wN+iggAYLCtfggSAGDDEcAAAAYTwAAABhPAAAAGE8AAAAYTwAAABhPAAAAG+/+jvoxn6v262gAAAABJRU5ErkJggg==\n",
      "text/plain": [
       "<Figure size 720x576 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "fig, ax = plt.subplots(figsize=(10, 8))\n",
    "fb_likes_cap.hist(ax=ax)\n",
    "fig.savefig('/tmp/c7-hist2.png', dpi=300)     # doctest: +SKIP"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### How it works..."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### There's more..."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 89,
   "metadata": {
    "lines_to_next_cell": 2
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 89,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "fb_likes_cap2 = fb_likes.clip(lower=300, upper=20000)\n",
    "fb_likes_cap2.equals(fb_likes_cap)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Masking DataFrame rows"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### How to do it..."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 90,
   "metadata": {
    "lines_to_next_cell": 2
   },
   "outputs": [],
   "source": [
    "movie = pd.read_csv('data/movie.csv', index_col='movie_title')\n",
    "c1 = movie['title_year'] >= 2010\n",
    "c2 = movie['title_year'].isna()\n",
    "criteria = c1 | c2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 91,
   "metadata": {
    "lines_to_next_cell": 2
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>color</th>\n",
       "      <th>director_name</th>\n",
       "      <th>...</th>\n",
       "      <th>aspect_ratio</th>\n",
       "      <th>movie_facebook_likes</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>movie_title</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>Avatar</th>\n",
       "      <td>Color</td>\n",
       "      <td>James Ca...</td>\n",
       "      <td>...</td>\n",
       "      <td>1.78</td>\n",
       "      <td>33000.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Pirates of the Caribbean: At World's End</th>\n",
       "      <td>Color</td>\n",
       "      <td>Gore Ver...</td>\n",
       "      <td>...</td>\n",
       "      <td>2.35</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Spectre</th>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>...</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>The Dark Knight Rises</th>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>...</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Star Wars: Episode VII - The Force Awakens</th>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>...</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>5 rows × 27 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "              color director_name  ...  aspect_ratio  movie_facebook_likes\n",
       "movie_title                        ...                                    \n",
       "Avatar        Color  James Ca...   ...         1.78       33000.0         \n",
       "Pirates o...  Color  Gore Ver...   ...         2.35           0.0         \n",
       "Spectre         NaN          NaN   ...          NaN           NaN         \n",
       "The Dark ...    NaN          NaN   ...          NaN           NaN         \n",
       "Star Wars...    NaN          NaN   ...          NaN           NaN         \n",
       "\n",
       "[5 rows x 27 columns]"
      ]
     },
     "execution_count": 91,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "movie.mask(criteria).head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 92,
   "metadata": {
    "lines_to_next_cell": 2
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>color</th>\n",
       "      <th>director_name</th>\n",
       "      <th>...</th>\n",
       "      <th>aspect_ratio</th>\n",
       "      <th>movie_facebook_likes</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>movie_title</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>Avatar</th>\n",
       "      <td>Color</td>\n",
       "      <td>James Ca...</td>\n",
       "      <td>...</td>\n",
       "      <td>1.78</td>\n",
       "      <td>33000.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Pirates of the Caribbean: At World's End</th>\n",
       "      <td>Color</td>\n",
       "      <td>Gore Ver...</td>\n",
       "      <td>...</td>\n",
       "      <td>2.35</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Spider-Man 3</th>\n",
       "      <td>Color</td>\n",
       "      <td>Sam Raimi</td>\n",
       "      <td>...</td>\n",
       "      <td>2.35</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Harry Potter and the Half-Blood Prince</th>\n",
       "      <td>Color</td>\n",
       "      <td>David Yates</td>\n",
       "      <td>...</td>\n",
       "      <td>2.35</td>\n",
       "      <td>10000.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Superman Returns</th>\n",
       "      <td>Color</td>\n",
       "      <td>Bryan Si...</td>\n",
       "      <td>...</td>\n",
       "      <td>2.35</td>\n",
       "      <td>0.0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>5 rows × 27 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "              color director_name  ...  aspect_ratio  movie_facebook_likes\n",
       "movie_title                        ...                                    \n",
       "Avatar        Color  James Ca...   ...         1.78       33000.0         \n",
       "Pirates o...  Color  Gore Ver...   ...         2.35           0.0         \n",
       "Spider-Man 3  Color    Sam Raimi   ...         2.35           0.0         \n",
       "Harry Pot...  Color  David Yates   ...         2.35       10000.0         \n",
       "Superman ...  Color  Bryan Si...   ...         2.35           0.0         \n",
       "\n",
       "[5 rows x 27 columns]"
      ]
     },
     "execution_count": 92,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "movie_mask = (movie\n",
    "    .mask(criteria)\n",
    "    .dropna(how='all')\n",
    ")\n",
    "movie_mask.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 93,
   "metadata": {
    "lines_to_next_cell": 2
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "False"
      ]
     },
     "execution_count": 93,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "movie_boolean = movie[movie['title_year'] < 2010]\n",
    "movie_mask.equals(movie_boolean)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 94,
   "metadata": {
    "lines_to_next_cell": 2
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 94,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "movie_mask.shape == movie_boolean.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 95,
   "metadata": {
    "lines_to_next_cell": 2
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "color                       True\n",
       "director_name               True\n",
       "num_critic_for_reviews      True\n",
       "duration                    True\n",
       "director_facebook_likes     True\n",
       "                           ...  \n",
       "title_year                  True\n",
       "actor_2_facebook_likes      True\n",
       "imdb_score                  True\n",
       "aspect_ratio                True\n",
       "movie_facebook_likes       False\n",
       "Length: 27, dtype: bool"
      ]
     },
     "execution_count": 95,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "movie_mask.dtypes == movie_boolean.dtypes"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 96,
   "metadata": {
    "lines_to_next_cell": 2
   },
   "outputs": [],
   "source": [
    "from pandas.testing import assert_frame_equal\n",
    "assert_frame_equal(movie_boolean, movie_mask,\n",
    "       check_dtype=False)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### How it works..."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### There's more..."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 97,
   "metadata": {
    "lines_to_next_cell": 2
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "35.9 ms ± 3 ms per loop (mean ± std. dev. of 7 runs, 10 loops each)\n"
     ]
    }
   ],
   "source": [
    "%timeit movie.mask(criteria).dropna(how='all')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 98,
   "metadata": {
    "lines_to_next_cell": 2
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1.91 ms ± 50.6 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)\n"
     ]
    }
   ],
   "source": [
    "%timeit movie[movie['title_year'] < 2010]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Selecting with booleans, integer location, and labels"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### How to do it..."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 99,
   "metadata": {
    "lines_to_next_cell": 2
   },
   "outputs": [],
   "source": [
    "movie = pd.read_csv('data/movie.csv', index_col='movie_title')\n",
    "c1 = movie['content_rating'] == 'G'\n",
    "c2 = movie['imdb_score'] < 4\n",
    "criteria = c1 & c2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 100,
   "metadata": {
    "lines_to_next_cell": 2
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>color</th>\n",
       "      <th>director_name</th>\n",
       "      <th>...</th>\n",
       "      <th>aspect_ratio</th>\n",
       "      <th>movie_facebook_likes</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>movie_title</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>The True Story of Puss'N Boots</th>\n",
       "      <td>Color</td>\n",
       "      <td>Jérôme D...</td>\n",
       "      <td>...</td>\n",
       "      <td>NaN</td>\n",
       "      <td>90</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Doogal</th>\n",
       "      <td>Color</td>\n",
       "      <td>Dave Bor...</td>\n",
       "      <td>...</td>\n",
       "      <td>1.85</td>\n",
       "      <td>346</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Thomas and the Magic Railroad</th>\n",
       "      <td>Color</td>\n",
       "      <td>Britt Al...</td>\n",
       "      <td>...</td>\n",
       "      <td>1.85</td>\n",
       "      <td>663</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Barney's Great Adventure</th>\n",
       "      <td>Color</td>\n",
       "      <td>Steve Gomer</td>\n",
       "      <td>...</td>\n",
       "      <td>1.85</td>\n",
       "      <td>436</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Justin Bieber: Never Say Never</th>\n",
       "      <td>Color</td>\n",
       "      <td>Jon M. Chu</td>\n",
       "      <td>...</td>\n",
       "      <td>1.85</td>\n",
       "      <td>62000</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>5 rows × 27 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "              color director_name  ...  aspect_ratio  movie_facebook_likes\n",
       "movie_title                        ...                                    \n",
       "The True ...  Color  Jérôme D...   ...          NaN            90         \n",
       "Doogal        Color  Dave Bor...   ...         1.85           346         \n",
       "Thomas an...  Color  Britt Al...   ...         1.85           663         \n",
       "Barney's ...  Color  Steve Gomer   ...         1.85           436         \n",
       "Justin Bi...  Color   Jon M. Chu   ...         1.85         62000         \n",
       "\n",
       "[5 rows x 27 columns]"
      ]
     },
     "execution_count": 100,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "movie_loc = movie.loc[criteria]\n",
    "movie_loc.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 101,
   "metadata": {
    "lines_to_next_cell": 2
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 101,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "movie_loc.equals(movie[criteria])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 102,
   "metadata": {
    "lines_to_next_cell": 2
   },
   "outputs": [
    {
     "ename": "ValueError",
     "evalue": "iLocation based boolean indexing cannot use an indexable as a mask",
     "output_type": "error",
     "traceback": [
      "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[0;31mValueError\u001b[0m                                Traceback (most recent call last)",
      "\u001b[0;32m<ipython-input-102-561fbb8e7e3f>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mmovie_iloc\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mmovie\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0miloc\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0mcriteria\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[0;32m~/.env/364/lib/python3.6/site-packages/pandas/core/indexing.py\u001b[0m in \u001b[0;36m__getitem__\u001b[0;34m(self, key)\u001b[0m\n\u001b[1;32m   1422\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m   1423\u001b[0m             \u001b[0mmaybe_callable\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mcom\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mapply_if_callable\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mkey\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mobj\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m-> 1424\u001b[0;31m             \u001b[0;32mreturn\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_getitem_axis\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mmaybe_callable\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0maxis\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0maxis\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m   1425\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m   1426\u001b[0m     \u001b[0;32mdef\u001b[0m \u001b[0m_is_scalar_access\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mkey\u001b[0m\u001b[0;34m:\u001b[0m \u001b[0mTuple\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
      "\u001b[0;32m~/.env/364/lib/python3.6/site-packages/pandas/core/indexing.py\u001b[0m in \u001b[0;36m_getitem_axis\u001b[0;34m(self, key, axis)\u001b[0m\n\u001b[1;32m   2141\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m   2142\u001b[0m         \u001b[0;32mif\u001b[0m \u001b[0mcom\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mis_bool_indexer\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mkey\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m-> 2143\u001b[0;31m             \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_validate_key\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mkey\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0maxis\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m   2144\u001b[0m             \u001b[0;32mreturn\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_getbool_axis\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mkey\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0maxis\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0maxis\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m   2145\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n",
      "\u001b[0;32m~/.env/364/lib/python3.6/site-packages/pandas/core/indexing.py\u001b[0m in \u001b[0;36m_validate_key\u001b[0;34m(self, key, axis)\u001b[0m\n\u001b[1;32m   2004\u001b[0m                     )\n\u001b[1;32m   2005\u001b[0m                 raise ValueError(\n\u001b[0;32m-> 2006\u001b[0;31m                     \u001b[0;34m\"iLocation based boolean indexing cannot use \"\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m   2007\u001b[0m                     \u001b[0;34m\"an indexable as a mask\"\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m   2008\u001b[0m                 )\n",
      "\u001b[0;31mValueError\u001b[0m: iLocation based boolean indexing cannot use an indexable as a mask"
     ]
    }
   ],
   "source": [
    "movie_iloc = movie.iloc[criteria]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 103,
   "metadata": {
    "lines_to_next_cell": 2
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 103,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "movie_iloc = movie.iloc[criteria.values]\n",
    "movie_iloc.equals(movie_loc)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 104,
   "metadata": {
    "lines_to_next_cell": 2
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "color                      False\n",
       "director_name              False\n",
       "num_critic_for_reviews     False\n",
       "duration                   False\n",
       "director_facebook_likes    False\n",
       "dtype: bool"
      ]
     },
     "execution_count": 104,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "criteria_col = movie.dtypes == np.int64\n",
    "criteria_col.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 105,
   "metadata": {
    "lines_to_next_cell": 2
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>num_voted_users</th>\n",
       "      <th>cast_total_facebook_likes</th>\n",
       "      <th>movie_facebook_likes</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>movie_title</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>Avatar</th>\n",
       "      <td>886204</td>\n",
       "      <td>4834</td>\n",
       "      <td>33000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Pirates of the Caribbean: At World's End</th>\n",
       "      <td>471220</td>\n",
       "      <td>48350</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Spectre</th>\n",
       "      <td>275868</td>\n",
       "      <td>11700</td>\n",
       "      <td>85000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>The Dark Knight Rises</th>\n",
       "      <td>1144337</td>\n",
       "      <td>106759</td>\n",
       "      <td>164000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Star Wars: Episode VII - The Force Awakens</th>\n",
       "      <td>8</td>\n",
       "      <td>143</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "              num_voted_users  cast_total_facebook_likes  movie_facebook_likes\n",
       "movie_title                                                                   \n",
       "Avatar             886204             4834                      33000         \n",
       "Pirates o...       471220            48350                          0         \n",
       "Spectre            275868            11700                      85000         \n",
       "The Dark ...      1144337           106759                     164000         \n",
       "Star Wars...            8              143                          0         "
      ]
     },
     "execution_count": 105,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "movie.loc[:, criteria_col].head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 106,
   "metadata": {
    "lines_to_next_cell": 2
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>num_voted_users</th>\n",
       "      <th>cast_total_facebook_likes</th>\n",
       "      <th>movie_facebook_likes</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>movie_title</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>Avatar</th>\n",
       "      <td>886204</td>\n",
       "      <td>4834</td>\n",
       "      <td>33000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Pirates of the Caribbean: At World's End</th>\n",
       "      <td>471220</td>\n",
       "      <td>48350</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Spectre</th>\n",
       "      <td>275868</td>\n",
       "      <td>11700</td>\n",
       "      <td>85000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>The Dark Knight Rises</th>\n",
       "      <td>1144337</td>\n",
       "      <td>106759</td>\n",
       "      <td>164000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Star Wars: Episode VII - The Force Awakens</th>\n",
       "      <td>8</td>\n",
       "      <td>143</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "              num_voted_users  cast_total_facebook_likes  movie_facebook_likes\n",
       "movie_title                                                                   \n",
       "Avatar             886204             4834                      33000         \n",
       "Pirates o...       471220            48350                          0         \n",
       "Spectre            275868            11700                      85000         \n",
       "The Dark ...      1144337           106759                     164000         \n",
       "Star Wars...            8              143                          0         "
      ]
     },
     "execution_count": 106,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "movie.iloc[:, criteria_col.values].head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 107,
   "metadata": {
    "lines_to_next_cell": 2
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>content_rating</th>\n",
       "      <th>imdb_score</th>\n",
       "      <th>title_year</th>\n",
       "      <th>gross</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>movie_title</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>Justin Bieber: Never Say Never</th>\n",
       "      <td>G</td>\n",
       "      <td>1.6</td>\n",
       "      <td>2011.0</td>\n",
       "      <td>73000942.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Sunday School Musical</th>\n",
       "      <td>G</td>\n",
       "      <td>2.5</td>\n",
       "      <td>2008.0</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Doogal</th>\n",
       "      <td>G</td>\n",
       "      <td>2.8</td>\n",
       "      <td>2006.0</td>\n",
       "      <td>7382993.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Barney's Great Adventure</th>\n",
       "      <td>G</td>\n",
       "      <td>2.8</td>\n",
       "      <td>1998.0</td>\n",
       "      <td>11144518.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>The True Story of Puss'N Boots</th>\n",
       "      <td>G</td>\n",
       "      <td>2.9</td>\n",
       "      <td>2009.0</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Thomas and the Magic Railroad</th>\n",
       "      <td>G</td>\n",
       "      <td>3.6</td>\n",
       "      <td>2000.0</td>\n",
       "      <td>15911333.0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "             content_rating  imdb_score  title_year       gross\n",
       "movie_title                                                    \n",
       "Justin Bi...            G           1.6      2011.0  73000942.0\n",
       "Sunday Sc...            G           2.5      2008.0         NaN\n",
       "Doogal                  G           2.8      2006.0   7382993.0\n",
       "Barney's ...            G           2.8      1998.0  11144518.0\n",
       "The True ...            G           2.9      2009.0         NaN\n",
       "Thomas an...            G           3.6      2000.0  15911333.0"
      ]
     },
     "execution_count": 107,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "cols = ['content_rating', 'imdb_score', 'title_year', 'gross']\n",
    "movie.loc[criteria, cols].sort_values('imdb_score')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 108,
   "metadata": {
    "lines_to_next_cell": 2
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[20, 24, 22, 8]"
      ]
     },
     "execution_count": 108,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "col_index = [movie.columns.get_loc(col) for col in cols]\n",
    "col_index"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 109,
   "metadata": {
    "lines_to_next_cell": 2
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>content_rating</th>\n",
       "      <th>imdb_score</th>\n",
       "      <th>title_year</th>\n",
       "      <th>gross</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>movie_title</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>Justin Bieber: Never Say Never</th>\n",
       "      <td>G</td>\n",
       "      <td>1.6</td>\n",
       "      <td>2011.0</td>\n",
       "      <td>73000942.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Sunday School Musical</th>\n",
       "      <td>G</td>\n",
       "      <td>2.5</td>\n",
       "      <td>2008.0</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Doogal</th>\n",
       "      <td>G</td>\n",
       "      <td>2.8</td>\n",
       "      <td>2006.0</td>\n",
       "      <td>7382993.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Barney's Great Adventure</th>\n",
       "      <td>G</td>\n",
       "      <td>2.8</td>\n",
       "      <td>1998.0</td>\n",
       "      <td>11144518.0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>The True Story of Puss'N Boots</th>\n",
       "      <td>G</td>\n",
       "      <td>2.9</td>\n",
       "      <td>2009.0</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Thomas and the Magic Railroad</th>\n",
       "      <td>G</td>\n",
       "      <td>3.6</td>\n",
       "      <td>2000.0</td>\n",
       "      <td>15911333.0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "             content_rating  imdb_score  title_year       gross\n",
       "movie_title                                                    \n",
       "Justin Bi...            G           1.6      2011.0  73000942.0\n",
       "Sunday Sc...            G           2.5      2008.0         NaN\n",
       "Doogal                  G           2.8      2006.0   7382993.0\n",
       "Barney's ...            G           2.8      1998.0  11144518.0\n",
       "The True ...            G           2.9      2009.0         NaN\n",
       "Thomas an...            G           3.6      2000.0  15911333.0"
      ]
     },
     "execution_count": 109,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "movie.iloc[criteria.values, col_index].sort_values('imdb_score')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### How it works..."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 110,
   "metadata": {
    "lines_to_next_cell": 2
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([False, False, False, False, False])"
      ]
     },
     "execution_count": 110,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a = criteria.values\n",
    "a[:5]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 111,
   "metadata": {
    "lines_to_next_cell": 2
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(4916, 4916)"
      ]
     },
     "execution_count": 111,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "len(a), len(criteria)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 112,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>num_voted_users</th>\n",
       "      <th>cast_total_facebook_likes</th>\n",
       "      <th>movie_facebook_likes</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>movie_title</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>Avatar</th>\n",
       "      <td>886204</td>\n",
       "      <td>4834</td>\n",
       "      <td>33000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Pirates of the Caribbean: At World's End</th>\n",
       "      <td>471220</td>\n",
       "      <td>48350</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Spectre</th>\n",
       "      <td>275868</td>\n",
       "      <td>11700</td>\n",
       "      <td>85000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>The Dark Knight Rises</th>\n",
       "      <td>1144337</td>\n",
       "      <td>106759</td>\n",
       "      <td>164000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Star Wars: Episode VII - The Force Awakens</th>\n",
       "      <td>8</td>\n",
       "      <td>143</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Signed Sealed Delivered</th>\n",
       "      <td>629</td>\n",
       "      <td>2283</td>\n",
       "      <td>84</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>The Following</th>\n",
       "      <td>73839</td>\n",
       "      <td>1753</td>\n",
       "      <td>32000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>A Plague So Pleasant</th>\n",
       "      <td>38</td>\n",
       "      <td>0</td>\n",
       "      <td>16</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Shanghai Calling</th>\n",
       "      <td>1255</td>\n",
       "      <td>2386</td>\n",
       "      <td>660</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>My Date with Drew</th>\n",
       "      <td>4285</td>\n",
       "      <td>163</td>\n",
       "      <td>456</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>4916 rows × 3 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "              num_voted_users  cast_total_facebook_likes  movie_facebook_likes\n",
       "movie_title                                                                   \n",
       "Avatar             886204             4834                      33000         \n",
       "Pirates o...       471220            48350                          0         \n",
       "Spectre            275868            11700                      85000         \n",
       "The Dark ...      1144337           106759                     164000         \n",
       "Star Wars...            8              143                          0         \n",
       "...                   ...              ...                        ...         \n",
       "Signed Se...          629             2283                         84         \n",
       "The Follo...        73839             1753                      32000         \n",
       "A Plague ...           38                0                         16         \n",
       "Shanghai ...         1255             2386                        660         \n",
       "My Date w...         4285              163                        456         \n",
       "\n",
       "[4916 rows x 3 columns]"
      ]
     },
     "execution_count": 112,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "movie.select_dtypes(int)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "jupytext": {
   "cell_metadata_filter": "-all",
   "main_language": "python",
   "notebook_metadata_filter": "-all"
  },
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
